package leetcode.code0648;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import leetcode.IDebug;

public class Solution2 implements IDebug {

	public String replaceWords(List<String> dictionary, String sentence) {
		Set<String> hash = new HashSet<>();
		for (int i = 0; i < dictionary.size(); i++) {
			hash.add(dictionary.get(i));
		}
		String[] words = sentence.split(" ");
		StringBuffer ans = new StringBuffer();
		for (int i = 0; i < words.length; i++) {
			char[] word = words[i].toCharArray();
			String cur = words[i];
			for (int j = 0; j < word.length - 1; j++) {
				String pre = new String(word, 0, j + 1);
				if (hash.contains(pre)) {
					cur = pre;
					break;
				}
			}
			ans.append(cur);
			if (i < words.length - 1) {
				ans.append(" ");
			}
		}
		return ans.toString();
	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution2 so = new Solution2();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
